#include <iostream>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
 
using namespace std;
typedef long long ll;
const int N = 105;
const ll inf = 1e11;
ll edge[N][N];

void init(){
	for(int i=0;i<N;i++){
		for(int j=0;j<N;j++){
			edge[i][j] = inf;
		}
	}
	for(int i=0;i<N;i++){
		edge[i][i] = 0;
	}
}
void add(int u, int v, ll w){
	if(w < edge[u][v]){
		edge[u][v] = w;
		edge[v][u] = w;
	}
}

void FloydWarshall(int n, int K){//On^3
	for(int k=1;k<=K;k++){
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(edge[i][k] + edge[k][j] < edge[i][j]){
					edge[i][j] = edge[i][k] + edge[k][j];
				}
			}
		}
	}
}

int main() {
    ll t, n, m, K, u, v, w, q;
    scanf("%lld", &t);
    while(t--){
    	init();
		scanf("%lld %lld %lld", &n, &m, &K);
		for(int i=0;i<m;i++){
			scanf("%lld %lld %lld", &u, &v, &w);
			add(u, v, w);
		}
		scanf("%lld", &q);
		FloydWarshall(n, K);
		for(int i=0;i<q;i++){
			scanf("%lld %lld", &u, &v);
			if(edge[u][v] == inf){
				printf("QAQ\n");
			}else{
				printf("%lld\n", edge[u][v]);
			}
		}
	}
    return 0;
}
